package flc.ast.activity;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.bumptech.glide.Glide;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityPictureCompressBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;

public class PictureCompressActivity extends BaseAc<ActivityPictureCompressBinding> {

    public static String imgPath;
    private int rate = 10;
    private String format = "";

    @Override
    protected int onCreate() {
        return R.layout.activity_picture_compress;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPictureCompressBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvCompressRate1.setOnClickListener(this);
        mDataBinding.tvCompressRate2.setOnClickListener(this);
        mDataBinding.tvCompressRate3.setOnClickListener(this);
        mDataBinding.tvCompressRate4.setOnClickListener(this);
        mDataBinding.tvCompressRate5.setOnClickListener(this);
        mDataBinding.tvCompressRate6.setOnClickListener(this);
        mDataBinding.tvCompressFormat1.setOnClickListener(this);
        mDataBinding.tvCompressFormat2.setOnClickListener(this);
        mDataBinding.tvCompressFormat3.setOnClickListener(this);
        mDataBinding.tvCompressFormat4.setOnClickListener(this);
        mDataBinding.tvStartCompress.setOnClickListener(this);

        Glide.with(mContext).load(imgPath).into(mDataBinding.ivCompressImg);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            //图片压缩率
            case R.id.tvCompressRate1://10%
                clearRateSelection();
                mDataBinding.tvCompressRate1.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 10;
                break;
            case R.id.tvCompressRate2://30%
                clearRateSelection();
                mDataBinding.tvCompressRate2.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 30;
                break;
            case R.id.tvCompressRate3://45%
                clearRateSelection();
                mDataBinding.tvCompressRate3.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 45;
                break;
            case R.id.tvCompressRate4://60%
                clearRateSelection();
                mDataBinding.tvCompressRate4.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 60;
                break;
            case R.id.tvCompressRate5://75%
                clearRateSelection();
                mDataBinding.tvCompressRate5.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 75;
                break;
            case R.id.tvCompressRate6://100%
                clearRateSelection();
                mDataBinding.tvCompressRate6.setBackgroundColor(Color.parseColor("#4E70EF"));
                rate = 100;
                break;
            //图片格式
            case R.id.tvCompressFormat1://原格式
                clearFormatSelection();
                mDataBinding.tvCompressFormat1.setBackgroundColor(Color.parseColor("#4E70EF"));
                format = "." + FileUtils.getFileExtension(imgPath);
                break;
            case R.id.tvCompressFormat2://jpg
                clearFormatSelection();
                mDataBinding.tvCompressFormat2.setBackgroundColor(Color.parseColor("#4E70EF"));
                format = getString(R.string.unit_jpg);
                break;
            case R.id.tvCompressFormat3://png
                clearFormatSelection();
                mDataBinding.tvCompressFormat3.setBackgroundColor(Color.parseColor("#4E70EF"));
                format = getString(R.string.unit_png);
                break;
            case R.id.tvCompressFormat4://webp
                clearFormatSelection();
                mDataBinding.tvCompressFormat4.setBackgroundColor(Color.parseColor("#4E70EF"));
                format = getString(R.string.unit_webp);
                break;
            case R.id.tvStartCompress://开始压缩
                save();
                break;

        }
    }

    @Override
    protected void initData() {
        format = "." + FileUtils.getFileExtension(imgPath);
    }

    private void save() {
        byte[] bytes = ImageUtils.compressByQuality(ImageUtils.getBitmap(imgPath), rate);
        Bitmap bitmap = ImageUtils.bytes2Bitmap(bytes);
        Bitmap.CompressFormat compressFormat;
        if (format.equals(getString(R.string.unit_jpg))) {
            compressFormat = Bitmap.CompressFormat.JPEG;
        } else if (format.equals(getString(R.string.unit_png))) {
            compressFormat = Bitmap.CompressFormat.PNG;
        } else if (format.equals(getString(R.string.unit_webp))) {
            compressFormat = Bitmap.CompressFormat.WEBP;
        } else {
            compressFormat = Bitmap.CompressFormat.PNG;
        }
        //保存到相册
        ImageUtils.save2Album(bitmap, compressFormat);
        //保存到最近文件
        String filePath = FileUtil.generateFilePath(Content.myCompress, format);
        ImageUtils.save(bitmap, filePath, compressFormat);
        CompressActivity.isSingle = true;
        CompressActivity.imgPath = imgPath;
        startActivity(CompressActivity.class);
    }

    private void clearRateSelection() {
        mDataBinding.tvCompressRate1.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressRate2.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressRate3.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressRate4.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressRate5.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressRate6.setBackgroundColor(Color.parseColor("#03091B"));
    }

    private void clearFormatSelection() {
        mDataBinding.tvCompressFormat1.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressFormat2.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressFormat3.setBackgroundColor(Color.parseColor("#03091B"));
        mDataBinding.tvCompressFormat4.setBackgroundColor(Color.parseColor("#03091B"));
    }

}